package com.thanhnx.tlg1;

import java.util.ArrayList;

import android.content.Context;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.ScaleGestureDetector;
import android.widget.ListView;

public class LinkListView extends ListView {
	private Context context;
	int offset;
	int oldVisibleItem = -1;
	int currentHeight;
	int prevHeight;

	public boolean cascadeScroll = true;
	public ArrayList<LinkListView> others = new ArrayList<LinkListView>();
	public LinkListView reference_lv;

	// Gesture
	private ScaleGestureDetector mScaleDetector;
	private float mScaleFactor = 1.f;

	public void setListenMultiTouch() {
		mScaleDetector = new ScaleGestureDetector(context, new ScaleListener());
	}

	public LinkListView(Context context) {
		super(context);
		this.context = context;
	}

	public LinkListView(Context context, AttributeSet attrs) {
		super(context, attrs);
		this.setVerticalFadingEdgeEnabled(false);
		this.context = context;
	}

	@Override
	public boolean onTouchEvent(MotionEvent ev) {
		super.onTouchEvent(ev);
		if (mScaleDetector != null) {
			mScaleDetector.onTouchEvent(ev);
		}
		return true;
	}

	@Override
	public void onDraw(Canvas canvas) {
		super.onDraw(canvas);

		canvas.save();
		canvas.scale(mScaleFactor, mScaleFactor);
//		if (mScaleFactor > 1.0) {
//			Log.e("redraw", "the listview");
//			this.draw(canvas);
//		}
		canvas.restore();
	}

	private class ScaleListener extends
			ScaleGestureDetector.SimpleOnScaleGestureListener {
		@Override
		public boolean onScale(ScaleGestureDetector detector) {
			mScaleFactor *= detector.getScaleFactor();
			// Don't let the object get too small or too large.
			mScaleFactor = Math.max(0.1f, Math.min(mScaleFactor, 5.0f));
			// TODO zoom here
			Log.e("On Scale", "mScaleFactor = " + mScaleFactor);
			invalidate();
			return true;
		}
	}

}
